package com.DHH.Controller.Device;

import com.DHH.Dto.FilterQuery;
import com.DHH.Dto.PageInfo;
import com.DHH.Entity.Device.Device;
import com.DHH.Entity.Device.DeviceArea;
import com.DHH.Mapper.Device.DeviceAreaMapper;

import com.DHH.Dto.QueryInfo;
import com.DHH.Mapper.Device.DeviceMapper;
import com.DHH.Util.ReflectionUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;




/**
 * Created by Administrator on 2017/4/15.
 */
@Controller
@RequestMapping("Admin/Device/Device")
public class DeviceController {
    private Logger logger= LoggerFactory.getLogger(DeviceController.class);
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private DeviceAreaMapper deviceAreaMapper;
 
    @RequestMapping("/listPage")
    public String listPage(HttpSession session,ModelMap modelMap){
        List<DeviceArea> deviceAreaList=deviceAreaMapper.selectAll();
        return "admin/device/listDevicePage.jsp";
    }
    @RequestMapping("/addPage")
    public String addPage(HttpSession session,ModelMap modelMap){
        List<DeviceArea> deviceAreaList=deviceAreaMapper.selectAll();
        modelMap.put("deviceAreaList",deviceAreaList);
        return "admin/device/addDevicePage.jsp";
    }
    @RequestMapping("/updatePage")
    public String updatePage(HttpSession session,@RequestParam Integer id,ModelMap modelMap)
    {
        Device device=new Device();
        device.setId(id);
        modelMap.put("device",deviceMapper.selectByPrimaryKey(device));
        return "admin/device/updateDevicePage.jsp";
    }
    @RequestMapping("/detailsPage")
    public String detailPage(HttpSession session,@RequestParam Integer id,ModelMap modelMap)
    {
        return "admin/device/detailsDevicePage.jsp";
    }

    @ResponseBody
    @RequestMapping("/query")
    public PageInfo<Device> query(QueryInfo queryInfo) {
        System.err.println("--------------查询参数---------------");
        System.err.println(queryInfo);
        System.err.println("--------------查询参数---------------");
        /*为查询设置统一分页条件和排序条件*/
        PageHelper.startPage(queryInfo.getCurrentPage(), queryInfo.getPageSize());
        PageHelper.orderBy(queryInfo.getSort() + " " + queryInfo.getOrder());
        if(queryInfo.getType()==null)
        {
            queryInfo.setType("search");
        }
        if(queryInfo.getCurrentPage()==null)
        {
            queryInfo.setCurrentPage(1);
        }
        if(queryInfo.getPageSize()==null)
        {
            queryInfo.setPageSize(10);
        }
        try{
            if(queryInfo.getType().equals("screen"))
            {
                return this.screen(queryInfo);
            }
            if(queryInfo.getType().equals("filter"))
            {

                return this.searchFilter(queryInfo);
            }
            return this.search(queryInfo);
        }catch (Exception e)
        {
            PageInfo<Device> p=new PageInfo<Device>();
            e.printStackTrace();
            p.setMsg("查询失败，请稍后重试！");
            return p;
        }

    }



    /**
     * 查询
     * @param queryInfo
     * @return
     */
    private PageInfo<Device> search(QueryInfo queryInfo)
    {   System.out.println("---------\n"+queryInfo);
        Device device=new Device();
            /*在这里设置相关的查询参数*/
        PageInfo<Device> pageInfo =new PageInfo<Device>();
        Page<Device> page = (Page<Device>) deviceMapper.searchByDevice(device);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
        return pageInfo;
    }

    /**
     * 筛选
     * @param queryInfo
     * @return
     */
    private  PageInfo<Device> screen(QueryInfo queryInfo) {
        PageInfo<Device> pageInfo=new PageInfo< Device>();
        Device device=new  Device();
        for (String key : queryInfo.getMap().keySet()) {
            ReflectionUtils.setFieldValue(device,key,queryInfo.getMap().get(key));
        }
        Page<Device> page = (Page<Device>) deviceMapper.screenByDevice(device);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
        return pageInfo;
    }

    /**
     * 过滤
     * @param queryInfo
     * @return
     */
    private PageInfo<Device> searchFilter(QueryInfo queryInfo) throws ParseException {    Device device=new  Device();
        PageInfo<Device> pageInfo=new PageInfo< Device>();
        FilterQuery filterQuery = new FilterQuery();
        filterQuery.setStartValue(queryInfo.getFilterStartValue());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        filterQuery.setEndValue(sdf.format(new Date(sdf.parse(queryInfo.getFilterEndValue()).getTime() + 24 * 3600 * 1000)));
        Page<Device> page = (Page<Device>)deviceMapper.filterByDevice(filterQuery);
        pageInfo.setRows(page.getResult());
        pageInfo.setTotalRow(page.getTotal());
        pageInfo.setCurrentPage(queryInfo.getCurrentPage());
        return pageInfo;
    }
}

